<template>
	<view class="page">
		<u-navbar title="我的亲属" border @leftClick="$utils.back()" placeholder />
		<view class="body">
			<z-paging @scroll='scroll' ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view v-for="item in data" :key="item.id" class="userItem">
						<image class="image" :src="item.relative.avatar"></image>
						<view class="flex">
							<view>
								{{item.remark_name?item.remark_name:'暂无备注'}}
							</view>
							<view>
								<u-text :text='item.relative.nick_name' :lines='1' size='30rpx' />
							</view>
							
						</view>
						<view @click.stop="()=>{}">
							<u-icon name="more-dot-fill" size="50rpx" @click='openSelect(item)'></u-icon>
						</view>
					</view>
				</template>
				<template v-else>
					<view v-for="item in 10" :key="item" class="userItem">
						<view class="image Skeleton"></view>
						<view class="flex">
							<view style="height: 40rpx;" class="Skeleton"></view>
							<view style="height: 30rpx;" class="Skeleton"></view>
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<view class="shareButton viewWidth" v-if="!loading">
			<view>
				邀请亲属
			</view>
			<button type="default" :plain='true' open-type="share" />
		</view>

		<root-portal>
			<u-modal :show="modalShow" :showCancelButton='true' @close='cancel' @cancel='cancel' @confirm='update'>
				<view style="width: 100%;">
					<view class="popupTitle">
						<u-icon name='close' color='#fff' />
						<view>
							编辑亲属
						</view>
						<u-icon name='close' @click='cancel' />
					</view>
					<view style="padding: 0 40rpx;">
						<u--form labelWidth='100' labelPosition="top" :model="form" :rules="rules" ref="uForm">
							<u-form-item label="备注:" prop="remark_name" borderBottom>
								<u--input v-model="form.remark_name" placeholder="请输入备注" clearable/>
							</u-form-item>
						</u--form>
					</view>
				</view>
			</u-modal>
			<u-modal :show="delShow" showCancelButton @confirm='del()' @cancel='delShow=false'>
				<view style="padding: 12px 25px 25px 25px;font-size: 30rpx;">
					是否确认删除
				</view>
			</u-modal>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				modalShow:false,
				form: {
					remark_name: null
				},
				rules: {
					'remark_name': {
						type: 'string',
						required: true,
						message: '请输入备注',
						trigger: ['blur', 'change']
					},
				},
				disabled:false,
				delShow: false,
				data: [],
				loading: true,
				selectShow: false,
				selectItem: null
			}
		},
		onShareAppMessage() {
			return {
				title: '亲友邀请',
				path: "/invite/index?id=" + this.user.id,
				imageUrl: this.user.avatar
			}

		},
		computed: {
			columns() {
				return [{
						name: '编辑'
					}, {
						name: '红包'
					},
					{
						name: '删除'
					},
					{
						name: '退出'
					},
				]
			},
			user() {
				return uni.getStorageSync('user')
			}
		},
		methods: {
			update() {
				if (this.disabled) return
				this.$refs.uForm.validate().then(res => {
					this.disabled = true
					this.$api.Relative.Update(this.selectItem.id, this.form).then(res => {
						uni.$u.toast('编辑成功')
						this.disabled = false
						this.modalShow = false
						this.$refs.paging.reload()
						this.cancel()
					}).catch(err => {
						this.disabled = false
						uni.$u.toast(err.data.message)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			cancel() {
				if (this.disabled) return
				this.$refs.uForm.resetFields()
				this.form = {
					remark_name: null
				}
				this.modalShow = false
			},
			del() {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api.Relative.Del(this.selectItem.id).then(err => {
					this.data = this.data.filter(item => item.id != this.selectItem.id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '红包') {
					this.$utils.navigateTo({
						url: '/hongbao/add?user=' + JSON.stringify(this.selectItem)
					})
				} else if(e.name=='退出'){
					this.selectShow=false
				} else if(e.name=='删除'){
					this.delShow=true
				}else if(e.name=='编辑'){
					this.form.remark_name=this.selectItem.remark_name
					this.modalShow=true
				}
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.Relative.List(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.relatives.data)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.userItem {
		display: flex;
		margin: 20rpx 0 0 0;
		position: relative;

		.image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 8rpx;
			margin-right: 20rpx;
		}

		.flex {
			height: 100rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding-bottom: 10rpx;
			box-sizing: border-box;
			border-bottom: 1rpx solid #eee;
			flex: 1;

		}

		>view:last-child {

			border-bottom: 1rpx solid #eee;
		}
	}

	.shareButton {
		border-radius: 100rpx;
		background-color: #ddd;
		text-align: center;
		padding: 20rpx;
		box-sizing: border-box;
		position: relative;

		>button {
			width: 100%;
			height: 100%;
			opacity: 0;
			position: absolute;
			z-index: 11;
			left: 0;
			top: 0;
		}
	}
</style>